<?php
/**
 * Created by PhpStorm.
 * User: fenfen
 * Date: 2016/12/26
 * Time: 14:26
 */

namespace frontend\controllers\bind;

use bc\controller\FrontController;
use bc\controller\WxOAuthController;
use bc\model\InputModel;
use bc\model\ItemdataModel;
use bc\model\OrderbindMode;
use yii\web\Session;
use bc\service\DictCityService;
use bc\service\DictDistrictService;
use bc\model\ProductsampleModel;
use bc\model\product\QrcodeModel;
//产品样本bind首页
class SampleController extends \bc\controller\SampleController
{
    private $PAGE = 1;

    public function actionIndex()
    {
        $product_id = $this->get('id',2);
        $this->getHtmlFromPid($product_id);
        $jsConfig = $this->configJsApi();
        
        $session = new Session();
        $user = $session->get('shop_user');
        
        if($product_id == 2)
        {
        	$view ['jsapi']['share_url']=$this->getRequest()->hostInfo.'/product/detail?id='.$product_id;
        	$view ['jsapi']['share_title']='滚蛋吧，肥胖君';
        	$view ['jsapi']['share_content']='找到DNA中的减肥机密';
        	
        	//是否代言人
        	$qrcodeModel = new QrcodeModel();
        	$qrcode = $qrcodeModel->getRow(['product_id' => $product_id, 'user_id' => $user['user_id'], 'is_delete' => 0]);
        	if($qrcode)
        	{
        		$view ['jsapi']['share_url']=$this->getRequest()->hostInfo.'/product/detail?id='. $product_id . '&c=' . $qrcode['code'];
        	}
        	
        }
	        
        
        
        
        $view['js_config'] = $jsConfig;
        $view ['product_html']=$this->html;
        $view ['product_id']=$product_id;
        return $this->render ( 'index' ,$view);
    }

    /*
     * 填写信息页
     * */
    public function actionInfo()
    {
        $bind_id = $this->get('bindid');
        $bind_no = $this->get('bindno');

        //获取属于哪一个产品
        $orderbindM = new OrderbindMode();
        $orderbindD=$orderbindM->getRowP(['bind_no'=>$bind_no]);
        $this->getHtmlFromPid($orderbindD['product_id']);
        // 获取省
//        $provinceService = new DictProvinceService ();
//        $provinceService->sort = 'id ASC';
//        $provinceList = $provinceService->getList ();
//        $view ['province'] = $provinceList;
//        $inputM = new inputModel();
//        $inputD = $inputM->getInputFromPage($this->PAGE);
//        $inputjson = $inputM->datahandling($inputD);
//        $view['data']=$inputjson;
        $view['bind_id']=$bind_id;
        $view['bind_no']=$bind_no;
        $view['product_id']=$orderbindD['product_id'];
        $view ['product_html']=$this->html;
        return $this->render ( 'info',$view );
    }

    /*
     * 绑定成功页
     * */
    public function actionSuccess()
    {
        $product_id = $this->get('product_id',2);
        $this->getHtmlFromPid($product_id);
        $view ['product_html']=$this->html;
        $view ['product_id']=$product_id;
        return $this->render ( 'success',$view );
    }

    public function actionTest()
    {
        return $this->render ( 'test' );
    }

    /*
     * 绑定扫码
     */
    public function actionScanqrcode()
    {
        $request = \Yii::$app->request;
        //获取数据
        $qrcode = $request->post('qrcode');
        $OrderBindModel = new OrderbindMode();
        $OrderBindModel->autoId='bind_id';
        //去数据表中查找记录
        $OrderBindModel->columns='bc_order_bind.*,bc_order_bind.user_id as obuser_id,bc_order.*,bc_order_detail.*,bc_product.*,bc_address.*,bc_wx_user.*,ecs_users.*,bc_order_status.*';
        $info = $OrderBindModel->getRow(['bind_no'=>$qrcode]);
        $infodata=$info[0];

        $session = new Session ();
        $shop_user = $session->get('shop_user');
        $shop_uesr_id = $shop_user['user_id'];
        if( $infodata )
        {
            if( $infodata['bind_status'] < 3 )
            {
                // 更新bind_status数据，给user_id添加值
                $OrderBindModel->modify(array('user_id'=>$shop_uesr_id,'bind_status'=>3),$infodata['bind_id']);
                $data   =   array(
                    'error'  =>    '0',
                    'msg'     =>  '绑定成功',
                    'info'    =>  $infodata
                );
            }else if($infodata['bind_status'] >= 3 && $shop_uesr_id == $infodata['obuser_id'])
            {
                //查看用户是否已经保存数据
                $itemdataM = new ItemdataModel();
                $itemdataD=$itemdataM->getRow(['bind_id'=>$infodata['bind_id']]);
                if(empty($itemdataD))
                {
                    $data   =   array(
                        'error'  =>    '0',
                        'msg'     =>  '绑定成功',
                        'info'    =>  $infodata
                    );
                }else{
                    $data   =   array(
                        'error'  =>    '1',
                        'msg'     =>  '你的扫码已经绑定,不能重复绑定！',
                        'info'    =>  $infodata
                    );
                }

            }else {
                $data = array(
                    'error' => '1',
                    'msg' => '绑定失败',
                    'info'    =>  $infodata
                );
            }
        }else{
            $data = array(
                'error' => '1',
                'msg' => '绑定失败'
            );
        }
//        var_dump($data);
//        die();
        echo json_encode($data);
    }
}